<?php 
class Property_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Property Model for tbl_property, tbl_address and tbl_company tables
	 * CRUD of Property Realated.
	 * This model include pagination and related functions.
	 * Refer detail in each function's comment section below.
	 * This model is related to property advertisements.
	 */
	// table name
	private $tbl_user = 'masteruser';
	private $tbl_property = 'property';
	private $tbl_type = 'property_type';
	private $tbl_address = 'property_address';

	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	/**
	 * count all the property related information
	 * @return [type] [description]
	 */
	function count_all(){
		return $this->db->count_all($this->$tbl_property);
	}
	/**
	 * Get the total number of current advetisement
	 * excluding that those are expired
	 * @param  [type] $cond [description]
	 * @return [type]       [description]
	 */
	function count_result($cond = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filter expiry date
		$this->db->where('expired_date > NOW()');
		
		$this->db->select('COUNT(*) as count', FALSE);
		$this->db->from($this->tbl_property);
		//$this->db->where('advid', $id);
		$this->db->join($this->tbl_user, 'masteruser.userID = property.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'property_type.id = property.typeID', 'left');

		$query = $this->db->get();
		$results = $query->result();
		$num_rows = $results[0]->count;
		$query->free_result();
		return $num_rows;
	}
	// get result based on conditions
	function get_result($cond = NULL, $limit = 10, $offset = 0, $sort = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filer expiry date
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		//limit & offset
		$this->db->limit($limit, $offset);

		$this->db->select('*', 'property_type.type', 'masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_property);
		//$this->db->where('advid', $id);
		$this->db->join($this->tbl_user, 'masteruser.userID = property.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'property_type.id = property.typeID', 'left');

		$query = $this->db->get();
		$results['rows'] = $query->result();
		$results['num_rows'] = $this->count_result($cond);
		$query->free_result(); //free result
		return $results;
	}
	// get by price range, cond will have 'low' and 'high' price range as array
	function get_by_pricerange($price_range = NULL, $cond=NULL, $limit = 10, $offset = 0, $sort = NULL){
	
		if ($price_range == NULL)
			return false;
		//condition
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//filter expiry date
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		
		//limit & offset
		$this->db->limit($limit, $offset);
		
		$this->db->select('*', 'property_type.type', 'masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_property);
		$this->db->where('price between ' . $price_range['low']. ' and ' . $price_range['high']);
		$this->db->join($this->tbl_user, 'masteruser.userID = property.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'property_type.id = property.typeID', 'left');
		
		$query = $this->db->get();
		$result = $query->result_array();
		$query->free_result(); //free result
		return $result;
	}
	/**
	 * get search result
	 * @param  [string]  $search [description]
	 * @param  [array]  $cond   [description]
	 * @param  [int] $limit  [description]
	 * @param  [int] $offset [description]
	 * @param  [array]  $sort   [description]
	 * @return [type]          [description]
	 */
	function get_search_result($search = NULL, $cond = NULL, $limit = 10, $offset = 0, $sort = NULL)
	{
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($this->tbl_property.'.'. $param , $item);
		}
		//filter expirty date
		$this->db->where('expired_date > NOW()');
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}

			//filter
		/**if ($othercond != NULL){
			foreach ($othercond as  $param => $item)
				//$this->db->where("property_address." . $param ." LIKE " . "'%" . $item ."%'");
				$this->db->or_like('property_address.' . $param, $item);
		}**/
		//limit & offset
		$this->db->limit($limit, $offset);
	
		$this->db->select('*', 'property_type.type', 'masteruser.firstName', 'masteruser.lastName');
		$this->db->from($this->tbl_property);
		if ($search){
			$this->db->where("(`title` LIKE  '%" . $search ."%' OR  `description` LIKE  '%". $search ."%')");
		}

		$this->db->join($this->tbl_user, 'masteruser.userID = property.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'property_type.id = property.typeID', 'left');
		//$this->db->join($this->tbl_address, 'property_address.addressID = property.address', 'left');
		$query = $this->db->get();
		log_message('INFO', "Query--->" . $this->db->last_query());
		$results['rows'] = $query->result();
		$results['num_rows'] = $this->count_search_result($search, $cond);
		$query->free_result(); //free result
		return $results;
	
	}
	/**
	get totoal number of search result in database
	return $count
	**/
	function count_search_result($search=NULL, $cond = NULL){
		//filter
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($this->tbl_property.'.'.$param , $item);
		}
		$this->db->where('expired_date > NOW()');
		$this->db->select('COUNT(*) as count');
		$this->db->from($this->tbl_property);
		if ($search){
			$this->db->where("(`title` LIKE  '%" . $search ."%' OR  `description` LIKE  '%". $search ."%')");
		}
		$this->db->join($this->tbl_user, 'masteruser.userID = property.advertiserID', 'left');
		$this->db->join($this->tbl_type, 'property_type.id = property.typeID', 'left');
		$query = $this->db->get();

		log_message('INFO', "Count Query--->" . $this->db->last_query());
		$results = $query->result();
		$num_rows = $results[0]->count;
		$query->free_result();
		return $num_rows;
	}
	// add new advertisement
	function save($property){
		//current & expiry date
		$current_date = date('Y-m-d H:m:s');
		$property['posted_date'] = $current_date;
		$this->load->helper('common_functions');
		$property['expired_date'] = calculate_expiry_date($current_date, PROPERTY_RETENSION);
		//save property object
		$this->db->insert($this->tbl_property, $property);
		return $this->db->insert_id();
	}
	// update person by userID
	function update($updated_property){
		//property informations
		$property['propertyID'] =$updated_property['propertyID'];
		$property['advertiserID'] = $updated_property['advertiserID'];
		$property['categoryID'] = $updated_property['categoryID'];
		$property['typeID'] = $updated_property['typeID'];

		$property['title'] = $updated_property['title'];
		$property['description'] = $updated_property['description'];
		$property['features'] = $updated_property['features'];
		$property['address'] = $updated_property['address'];

		$property['posted_date'] = $updated_property['posted_date'];
		$property['expired_date'] = $updated_property['expired_date'];
		//contact person information
		$property['contact_person'] = $updated_property['contact_person'];
		$property['price'] = $updated_property['price'];
		$property['is_negotiable'] = $updated_property['is_negotiable'];
		//update
		$this->db->where('propertyID',$updated_property['propertyID']);
		$this->db->update($this->tbl_property, $property);
	}
	// delete person by advertiserID
	function delete($id){
		$this->db->where('propertyID', $id);
		$this->db->delete($this->tbl_property);
	}
}
/* End of file property_model.php */
/* Location: ./application/models/property_model.php */

?>